package com.sali.排序.raw;

import java.util.Arrays;

/**
 * @description:
 * @author: sali
 * @date: 2025/9/14 下午4:10.
 * @version: 1.0
 */
public class raw3_插入排序 {

    public static void main(String[] args) {
        int[] arr = new int[]{5, 4, 2, 3, 1};
        System.out.println(Arrays.toString(myGetRes(arr)));
    }

    private static int[] myGetRes(int[] arr) {
        int sortedIndex = 0;
        while ( sortedIndex < arr.length ) {
            for ( int i = sortedIndex; i > 0; i-- ) {
                if ( arr[i] < arr[i-1] ) {
                    int tmp = arr[i];
                    arr[i] = arr[i-1];
                    arr[i-1] = tmp;
                } else {
                    break;
                }
            }
            sortedIndex++;
        }
        return arr;
    }

}
